package com.hbnu.jiani.dao.impl;

import com.hbnu.jiani.dao.PackageDao;
import com.hbnu.jiani.entity.Package;
import com.hbnu.jiani.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PackageDaoImpl implements PackageDao {

    @Override
    public boolean addPackage(Package pkg) {
        String sql = "INSERT INTO packages (id,recipient, pickup_code, status) VALUES (?,?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            stmt.setInt(1, pkg.getId());
            stmt.setString(2, pkg.getRecipient());
            stmt.setString(3, pkg.getPickupCode());
            stmt.setString(4, pkg.getStatus());
            int affected = stmt.executeUpdate();
            if (affected > 0) {
                try (ResultSet rs = stmt.getGeneratedKeys()) {
                    if (rs.next()) {
                        pkg.setId(rs.getInt(1));
                    }
                }
            }
            return affected > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deletePackageById(int id) {
        String sql = "DELETE FROM packages WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean updatePackage(Package pkg) {
        String sql = "UPDATE packages SET recipient = ?, pickup_code = ?, status = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, pkg.getRecipient());
            stmt.setString(2, pkg.getPickupCode());
            stmt.setString(3, pkg.getStatus());
            stmt.setInt(4, pkg.getId());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Package> findAll() {
        List<Package> list = new ArrayList<>();
        String sql = "SELECT id, recipient, pickup_code, status FROM packages";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql);
             ResultSet rs = stmt.executeQuery()) {
            while (rs.next()) {
                Package pkg = new Package();
                pkg.setId(rs.getInt("id"));
                pkg.setRecipient(rs.getString("recipient"));
                pkg.setPickupCode(rs.getString("pickup_code"));
                pkg.setStatus(rs.getString("status"));
                list.add(pkg);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Package findById(int id) {
        String sql = "SELECT id, recipient, pickup_code, status FROM packages WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    Package pkg = new Package();
                    pkg.setId(rs.getInt("id"));
                    pkg.setRecipient(rs.getString("recipient"));
                    pkg.setPickupCode(rs.getString("pickup_code"));
                    pkg.setStatus(rs.getString("status"));
                    return pkg;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Package> findByRecipient(String recipient) {
        List<Package> list = new ArrayList<>();
        String sql = "SELECT id, recipient, pickup_code, status FROM packages WHERE recipient = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, recipient);
            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Package pkg = new Package();
                    pkg.setId(rs.getInt("id"));
                    pkg.setRecipient(rs.getString("recipient"));
                    pkg.setPickupCode(rs.getString("pickup_code"));
                    pkg.setStatus(rs.getString("status"));
                    list.add(pkg);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean existsById(int id) {
        String sql = "SELECT 1 FROM packages WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                return rs.next();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean existsByPickupCode(String pickupCode) {
        String sql = "SELECT 1 FROM packages WHERE pickup_code = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, pickupCode);
            try (ResultSet rs = stmt.executeQuery()) {
                return rs.next(); // 如果查到，说明已存在
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
